﻿@{
    string _prefix = "Stock";
    var _treeId = _prefix + "Tree";
    var _gridId = _prefix + "Grid";
    var _treeDetail = _prefix + "Detail";
}

@{ Html.RenderAction("MenuHeader", "Home");}
<div class="bjui-pageContent tableContent">
    <div class="clearfix">
        <div style="float: left; width: 220px; overflow: auto;" class="table table-bordered">
            <ul id="@_treeId" class="ztree"></ul>
        </div>

        <div id="@_treeDetail" style="margin-left: 225px;">
        </div>
    </div>
</div>

<script type="text/javascript">
    var selectedId = 0;
    $(document).ready(function () {
        initZtree();
        loadDataGrid();
    });
    //加载数据到datagrid
    function loadDataGrid() {
        //b-jui的datagrid需要重新处理HTML
        $('#@_treeDetail').empty()
            .append('<table id="@_gridId" class="table table-bordered table-hover table-striped table-top"></table>');

        $('#@_gridId').datagrid({
            showToolbar: false,
            filterThead: false,
            columns: [
                {
                    name: 'Id',
                    label: '数据ID',
                    width: 100,
                    hide: true
                },
                {
                    name: 'Name',
                    label: '产品名称',
                    width: 100
                },
                {
                    name: 'Number',
                    label: '产品数量',
                    width: 100
                },
                {
                    name: 'Price',
                    label: '产品单价',
                    width: 100
                },
                {
                    name: 'Status',
                    label: '出库/入库',
                    width: 100
                      , type: 'select',
                    align: 'center',
                    items: [{ '0': '入库' }, { '1': '出库' }],
                },
                {
                    name: 'User',
                    label: '操作人',
                    width: 100
                },
                {
                    name: 'Time',
                    label: '操作时间',
                    width: 100
                     , type: 'date',
                    pattern: 'yyyy-MM-dd HH:mm:ss'
                },
                {
                    name: 'OrgId',
                    label: '所属部门',
                    width: 100,
                    hide: true
                }
            ],
            dataUrl: 'StockManager/Load?parentId=' + selectedId,
            fullGrid: true,
            showLinenumber: true,
            showCheckboxcol: true,
            paging: true,
            filterMult: false,
            showTfoot: true,
            height: '700'
        });
    }

    function zTreeOnClick(event, treeId, treeNode) {
        selectedId = treeNode.Id;
        loadDataGrid();
    }

    function initZtree() {
        var setting = {
            view: { selectedMulti: false },
            data: {
                key: {
                    name: 'Name',
                    title: 'Name'
                },
                simpleData: {
                    enable: true,
                    idKey: 'Id',
                    pIdKey: 'ParentId',
                    rootPId: 'null'
                }
            },
            callback: { onClick: zTreeOnClick }
        };
        $.getJSON('OrgManager/LoadOrg', function (json) {
            var zTreeObj = $.fn.zTree.init($('#@_treeId'), setting, json);
            zTreeObj.expandAll(true);
        });
    }

    //删除
    function delStock() {
        var selected = getSelected('#@_gridId', 2);
        if (selected == null) return;

        $.getJSON('StockManager/Delete?Id=' + selected, function (data) {
            if (data.statusCode == "200")
                loadDataGrid();
            else {
                $(this).alertmsg('warn', data.message);
            }
        });
    }

    //自定义的编辑按钮
    function editStock() {
        var selected = getSelected('#@_gridId', 2);
        if (selected == null) return;

        $(this).dialog({
            id: 'editDialog',
            url: '/StockManager/Add?id=' + selected,
            title: '编辑',
            onClose: function () {
                refreshStockGrid();
            }
        });

    }

    function refreshStockGrid() {
        $('#@_gridId').datagrid('refresh');
        // loadDataGrid();
    }
    //@@ sourceURL=StockManagerIndex.js
</script>